const axios = require('axios'),
    fs = require('fs')

let url = `https://www.kuke99.com/ucenter/course_detail/id/`,
    courseId = '2625'
axios({
    method: 'get',
    url: url + courseId,
    headers: {
        'Cookie': 'access_token=109ee6afa4fb5ac71bbe41cbafb66cdc'
    }
}).then(response => {
    // console.log(JSON.stringify(res.data).match(/(?<=\<div\sclass="directory-list-wrap"\>\<\/div\>)*(?=好课推荐)/g)[0])
    var res = JSON.stringify(response.data),
        title = res.match(/(?<=share-title=\\").*(?=\\"\sshare-url)/g)[0],
        start = res.search('课程目录'),
        end = res.search('<!--弹窗-->'),
        res = res.substring(start, end).replace(/(\s|\\n|\\|&nbsp;)/g, ''),
        sectionList = res.match(/(?<=\<!--章展开类名open-->).*(?=\<!--好课推荐--\>)/g)
    // 按中间内容字符串长度大小分段
    var min = 0,
        max = 7000,
        split = 100,
        regexp = '('
    for (; min < max; min += split) {
        regexp += `.{${min},${min+split}}|`
    }
    regexp = regexp.substring(0, regexp.lastIndexOf('|')) + ')'
    var Reg = new RegExp(`\<!--章展开类名open--\>${regexp}(\<!--章--\>|\<!--好课推荐--\>)`, 'g')
    // var Reg = new RegExp(`\<!--章展开类名open--\>${regexp}\<div\sclass="dis-middle\sicon-play"\>\<\/div\>`, 'g')
    sectionList = res.match(Reg)
    // console.log(Reg)
    // fs.writeFile(__dirname + '\\Data\\dir.txt', res, err => {
    //     if (err) console.error('写入文件失败')
    //     console.log(`课程号为${courseId}的课程目录写入成功`)
    // })
    console.log(title)
    // sectionList.map(v => {
    //     var sectionItemTitle = v.match(/(?<=zhang-listdirectory-list"\>\n\<divclass="fl"\>).{1,20}(?=\<\/div\>)/g)
    //     return
    // })
    var sectionItemTitle = sectionList[0].match(/(?<=zhang-listdirectory-list"\>\n\<divclass="fl"\>).{1,20}(?=\<\/div\>)/g)
    console.log(sectionList[0])
    console.log(sectionItemTitle)

    console.log(res.length)
    // console.log(__dirname + '\\Data\\dir.json')

})